package com.example.lastwork2.fragments;

import static android.app.Activity.RESULT_OK;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.example.lastwork2.R;
import com.example.lastwork2.cart.CartActivity;

import java.io.InputStream;
import java.util.zip.DeflaterInputStream;

public class MineFragment extends Fragment {


    private ImageView myHead;
    private Button myCart;


    @SuppressLint("ResourceType")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        //绑定布局文件
        View minePage = inflater.inflate(R.layout.mine_fragment,null);
        //

        //绑定控件
        myHead  = minePage.findViewById(R.id.my_head);
        myCart = minePage.findViewById(R.id.my_cart);



        //选择头像
        myHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK,null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
                //intent 待启动的Intent 100（requestCode）请求码，返回时用来区分是那次请求
                startActivityForResult(intent ,100);
            }
        });


        //点击订单跳转到订单页


        //点击购物车按钮跳转到购物车页面
        myCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(
                        getActivity(),
                        CartActivity.class
                );
                startActivity(intent);
            }
        });




        return minePage;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //返回成功，请求码（对应启动时的requestCode）
        if(resultCode == RESULT_OK && requestCode==100)
        {
            //方式二
            Uri uri = data.getData();
            ContentResolver cr = getContext().getContentResolver();
            try {
                //根据Uri获取流文件
                InputStream is = cr.openInputStream(uri);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize =3;
                Bitmap bitmap = BitmapFactory.decodeStream(is,null,options);
                myHead.setImageBitmap(bitmap);
            }
            catch(Exception e)
            {
                Log.i("lyf", e.toString());
            }
        }
        super.onActivityResult(requestCode, resultCode, data);


    }
}